package com.huonan.test.test2;

import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.date.TimeInterval;
import cn.hutool.core.io.FileUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.huonan.jb.treads.ThreadPool;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.jsoup.Jsoup;

import java.io.File;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;

/**
 * @Description 处理文件内容
 * @Author lxp
 * @Date 2021/11/23 上午10:13
 * @Version V1.0
 **/
public class FilteredFields {
    private static final Logger log = LogManager.getLogger(FilteredFields.class);
    private static final int MAXLINES = 100000;
    private static String fitPath = "";
    private static ThreadPoolExecutor threadPool;

    public static void main(String[] args) {
        threadPool = ThreadPool.getExecutorService();
        threadPool.setCorePoolSize(Integer.parseInt(args[0]));
        threadPool.setMaximumPoolSize(Integer.parseInt(args[1]));
        log.info("初始化线程信息：activeCount:{},coreSize:{},maxSize:{}", threadPool.getActiveCount(),
                threadPool.getCorePoolSize(), threadPool.getMaximumPoolSize());
        TimeInterval timer = DateUtil.timer();
        String sourcePath = args[2];
        fitPath = args[3];
        //第一级目录，遍历深度1
        // /data/bool/unzip/bool/regulation/20221203/ec/jd.com
        List<File> files1 = FileUtil.loopFiles(Paths.get(sourcePath), 1, null)
                .stream().filter(l -> !l.getPath().endsWith(".DS_Store")).collect(Collectors.toList());
        if (FileUtil.isDirectory(files1.get(0))) {
            files1.forEach(f1 -> {
                log.info("处理目录 多级：{}", f1);
                handleFiles(timer, sourcePath);
            });
        } else {
            log.info("处理目录一级：{}", sourcePath);
            handleFiles(timer, sourcePath);
        }
    }

    private static void handleFiles(TimeInterval timer, String sourcePath) {
        //文件
        List<File> listFiles = FileUtil.loopFiles(new File(sourcePath)).
                stream().filter(l -> !l.getPath().endsWith(".DS_Store")).collect(Collectors.toList());
        List<Future<String>> futures = new ArrayList<>();
        log.info("文件数量：{}", listFiles.size());
        listFiles.forEach(file -> {
            /*线程处理*/
            Future<String> future = threadPool.submit(() -> getIntegerFuture(timer, file));
            futures.add(future);
        });
        futures.forEach(f -> {
            try {
                f.get();
            } catch (InterruptedException | ExecutionException e) {
                e.printStackTrace();
            }
        });
        threadPool.shutdown();
    }

    private static void writeToFile(TimeInterval timer, AtomicInteger count, File file, String sb,
            int size) {
        log.info("====================");
        writeToFile(file, sb, count.get());
        log.info("文件：{},数据量：{},总耗时：{}",
                file.getPath(), size, timer.interval());
    }

    private static String getIntegerFuture(TimeInterval timer, File file) {
        //文件内容
        long fileMsize = FileUtil.size(file) / 1024 / 1024;
        List<String> lines = FileUtil.readLines(file, "UTF-8");
        log.info("文件大小：{} M,数据量：{} ", fileMsize, lines.size());
        List<List<String>> split = ListUtil.split(lines, MAXLINES);
        AtomicInteger splitCount = new AtomicInteger(split.size());
        StringBuilder finalSb = new StringBuilder();
        for (List<String> list : split) {
            try {
                for (String l : list) {
                    processingContent(finalSb, l);
                }
                writeToFile(timer, splitCount, file, finalSb.toString(), list.size());
                splitCount.getAndDecrement();
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                finalSb.setLength(0);
            }
        }
        return file.getName();
    }

    /*处理内容*/
    private static void processingContent(StringBuilder sb, String l) {
        String line = l.substring(l.indexOf("{"));
        JSONObject obj = JSONUtil.parseObj(line);
        String content = obj.getStr("content");
        String deleteContent = Jsoup.parse(content).text().replaceAll("u3000", " ");
        //TODO 去Unicode
        String id;
        String index;
        if (obj.getStr("cid") != null) {
            id = "0_bayou_c_" + obj.getStr("cid");
            String cPubDate = obj.getStr("pub_date");
            DateTime dateTime = DateUtil.parse(cPubDate);
            String pubDate = DateUtil.format(dateTime, "yyyyMM");
            index = "ik_sl_v2_" + pubDate + "_wenda";
        } else {
            id = "0_bayou_" + obj.getStr("aid");
            String pubTime = obj.getStr("pub_time");
            //ik_sl_v2_%{month}_%{type}
            DateTime dateTime = DateUtil.parse(pubTime);
            String pubDate = DateUtil.format(dateTime, "yyyyMM");
            index = "ik_sl_v2_" + pubDate + "_wenda";
        }
        JSONObject json1 = JSONUtil.createObj();
        json1.set("_index", index);
        json1.set("_id", id);
        json1.set("content", deleteContent);
        sb.append(JSONUtil.toJsonStr(json1)).append("\n");
    }

    private static void writeToFile(File file, String content, Integer count) {
        FileUtil.writeUtf8String(
                content, new File(fitPath + "/filtered/" + "/filter_" + file.getName() + "_" + count));
    }
}